<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="${comment}" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入${comment}"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->


    <el-table v-loading="loading" :data="commentList" @selection-change="handleSelectionChange" ref="commentTable">
      <el-table-column type="expand">
        <template slot-scope="props">
          <el-form label-position="left" inline class="demo-table-expand" v-if="props.row.expanded">
            <!-- 循环遍历回复列表 -->
            <template v-for="(reply, index) in props.row.dtsReplyList">
              <el-form-item v-if="reply.dtsAdmin && reply.dtsAdmin.avatar" :key="index">
                <div class="flex-container">
                  <image-preview :src="reply.dtsAdmin.avatar" :width="45" :height="45"/>
                  <div class="pDiv">
                    <span>{{reply.dtsAdmin.username}}于
                      【{{new Date(reply.dtsAdmin.addTime).toLocaleString()}}】回复：
                    </span>
                  </div>
                </div>
                <div class="hDiv">
                  <span>{{reply.replyContent}}</span> <!-- 假设回复内容在content属性，需根据实际调整 -->
                </div>
              </el-form-item>
            </template>
            <el-form-item label="回复内容" class="full-width">
              <div class="input-button-container">
                <!-- 添加 v-model 绑定 -->
                <el-input
                  type="textarea"
                  v-model="props.row.replyContent"
                  @input="onInput(props.row)"
                ></el-input>
                <div style="width: 20px;"></div>
                <el-button class="offset-button" type="primary" @click="submitForm(props.row)">确 定</el-button>
              </div>
            </el-form-item>
          </el-form>
        </template>
      </el-table-column>

      <el-table-column label="用户名称" align="center" prop="dtsUser.username" />
      <el-table-column label="评论商品" align="center" :show-overflow-tooltip="true" prop="dtsGoods.name" />
      <el-table-column label="评分" align="center" prop="star" />
      <el-table-column label="评论内容" align="center" :show-overflow-tooltip="true" prop="content" />
      <el-table-column label="评论图片" align="center" prop="hasPicture" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            type="primary"
            @click="toggleRowExpansion(scope.row)"
            v-hasPermi="['good:remark:comment:edit']"
          >
            回复
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品评论对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="如果type=0，则是商品评论；如果是type=1，则是专题评论。" prop="valueId">
          <el-input v-model="form.valueId" placeholder="请输入如果type=0，则是商品评论；如果是type=1，则是专题评论。" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="评分" prop="star">
          <el-input v-model="form.star" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="评论内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="评论图片" prop="hasPicture">
          <el-input v-model="form.hasPicture" placeholder="请输入评论图片" />
        </el-form-item>
        <el-form-item label="图片地址列表，采用JSON数组格式" prop="picUrls">
          <el-input v-model="form.picUrls" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="创建时间" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="逻辑删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入逻辑删除" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listComment, getComment, delComment, addComment, updateComment } from "@/api/good/remark/comment";
import { addReply } from "@/api/good/respond/reply";

export default {
  name: "Comment",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品评论表格数据
      commentList: [],
      reply:{
        id:null,
        commentId:null,
        adminId:null,
        replyContent:null,
        addTime:null,
        updateTime:null,
        deleted:null,
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        valueId: [
          { required: true, message: "如果type=0，则是商品评论；如果是type=1，则是专题评论。不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "评论类型，如果type=0，则是商品评论；如果是type=1，则是专题评论；如果type=3，则是订单商品评论。不能为空", trigger: "change" }
        ],
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "评论内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品评论列表 */
    getList() {
      this.loading = true;
      listComment(this.queryParams).then(response => {
        this.commentList = response.rows;
        // 初始化回复内容属性
        this.commentList.forEach(item => {
          item.replyContent = '';
          item.expanded = false; // 确保展开状态被正确初始化
        });
        this.total = response.total;
        this.loading = false;
        console.log(JSON.stringify(this.commentList, null, 2));
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        valueId: null,
        type: null,
        userId: null,
        star: null,
        content: null,
        hasPicture: null,
        picUrls: null,
        addTime: null,
        updateTime: null,
        deleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!== 1;
      this.multiple =!selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品评论";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      console.log(JSON.stringify(row, null, 2));
    },
    /** 提交按钮 */
    submitForm(row) {
      // console.log(JSON.stringify(row.replyContent, null, 2));
      // console.log(JSON.stringify(row, null, 2));
       this.reply.commentId=row.id;
       this.reply.adminId=1;
       this.reply.replyContent=row.replyContent;
       console.log(JSON.stringify(this.reply, null, 2));
      // 调用添加回复的接口
      // addReply(this.reply).then(response => {
      //   this.$modal.msgSuccess("回复成功");
      //   this.getList(); // 刷新列表
      // }).catch(error => {
      //   this.$modal.msgError("回复失败");
      // });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品评论编号为"' + ids + '"的数据项？').then(function () {
        return delComment(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('good/remark/comment/export', {
        ...this.queryParams
      }, `comment_${new Date().getTime()}.xlsx`);
    },
    toggleRowExpansion(row) {
      // 关闭其他已展开的行
      this.commentList.forEach(item => {
        if (item!== row) {
          item.expanded = false;
          this.$refs.commentTable.toggleRowExpansion(item, false);
        }
      });
      // 切换当前行的展开状态
      row.expanded =!row.expanded;
      this.$refs.commentTable.toggleRowExpansion(row);
    },
    onInput(row) {
      console.log('输入内容:', row.replyContent);
      // 手动触发更新，确保数据响应式更新
      this.$forceUpdate();
    }
  }
};
</script>

<style scoped>
/* 临时重置输入框样式 */
/deep/ .el-input__inner {
  color: #333;
  background-color: #fff;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  padding: 0 15px;
  font-size: 14px;
  line-height: 40px;
  outline: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}



/* 使用深度选择器确保样式生效 */
/deep/.full-width {
  display: block;
  width: 100%;
  margin-top: 10px; /* 可根据需要调整上下间距 */
  margin-bottom: 10px;
}

/* 设置输入框和按钮的容器样式 */
/deep/.input-button-container {
  display: flex;
  align-items: center;
}

.flex-container {
  display: flex;
  align-items: center;
  margin-left: 3px;
}

.pDiv{
  margin-left: 10px;
  margin-top: -20px;
  padding-left: 10px;
  padding-right: 10px;
  color: #9D9399;
  display: inline-block;
  line-height: 26px;
  background-color: #F4F4F5;
  font-size: 12px;
  border-radius: 3px;
  border: solid 1px #F4E9EB;
}

.hDiv{
  margin-left: 60px;
  margin-top: -20px;
}

/deep/ .el-table__expand-icon {
  display: none;
}

/* 为确定按钮添加偏移样式 */
/deep/.offset-button {
  margin-left: auto; /* 让按钮靠右 */
}

.demo-table-expand {
  display: flex; /* 设为弹性布局 */
  flex-direction: column; /* 更改弹性布局方向为垂直，实现内容上下排列 */
}

</style>
